package com.example.room_demo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.lifecycle.lifecycleScope
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class MainActivity : AppCompatActivity() {
    var database: DataBaseManager? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //初始化Room
        database = DataBaseManager.getDatabase(this)

        //添加数据
        btn_add.setOnClickListener {
            lifecycleScope.launch {
                val name = et_name.text.toString().trim()
                val gameId = et_gameId.text.toString().trim()
                if (name != "") {
                    val data = DataBean()
                    data.name = name//游戏名称
                    data.game_id = gameId//游戏ID
                    addData(data)//添加
//                    Log.e("TAG", "addData Main: 后执行")
                    //清空输入框
                    et_name.setText("")
                    et_gameId.setText("")
                }
            }
        }

        //查找所有数据
        btn_select.setOnClickListener {
            lifecycleScope.launch {
                val data = getData()
                data?.let {
                    for (name in data) {
                        Log.e(
                            "TAG",
                            "onCreate: " + name.id + "-" + name.name + "-" + name.game_id
                        )
                    }
                }
            }
        }

        //模糊查询
        btn_fuzzyQuery.setOnClickListener {
            lifecycleScope.launch {
                val name = et_name1.text.toString().trim()
                if (name != "") {
                    val data = fuzzyQuery(name)
                    data?.let {
                        for (name in data) {
                            Log.e(
                                "TAG",
                                "onCreate: " + name.id + "-" + name.name + "-" + name.game_id
                            )
                        }
                    }
                }

            }
        }

        //删除数据---按条件删除
        btn_delete.setOnClickListener {
            lifecycleScope.launch {
                val name = et_name1.text.toString().trim()
                if (name != "")
                    deleteData(name)
            }
        }

        //修改
        btn_upData.setOnClickListener {
            lifecycleScope.launch {
                val name = et_name.text.toString().trim()
                val newName = et_name1.text.toString().trim()
                if (name != "" && newName != "") {
                    upData(name, newName)
                }
            }
        }

        //删全部
        btn_deleteAll.setOnClickListener {
            lifecycleScope.launch {
                deleteAll()
            }
        }
    }

    //添加数据
    suspend fun addData(data: DataBean) {
        withContext(Dispatchers.IO) {
//            Log.e("TAG", "addData IO: 先执行")
            database!!.dataDao().insert(data)
        }
    }

    //获取数据
    suspend fun getData(): MutableList<DataBean>? {
        var data: MutableList<DataBean>?
        withContext(Dispatchers.IO) {
            data = database?.dataDao()?.getDatas()
        }
        return data
    }

    //删除数据---按条件删除
    suspend fun deleteData(name: String) {
        withContext(Dispatchers.IO) {
            val delete = database?.dataDao()?.delete(name)
            Log.e("TAG", "deleteData: $delete")
        }
    }

    //修改数据库
    suspend fun upData(name: String, newName: String) {
        withContext(Dispatchers.IO) {
            val i = database?.dataDao()?.update(name, newName)
            Log.e("TAG", "upData: $i")
        }
    }

    //清空表
    suspend fun deleteAll() {
        withContext(Dispatchers.IO) {
            val i = database?.dataDao()?.deleteAll()
            Log.e("TAG", "deleteAll: $i")

        }
    }

    //模糊查询
    suspend fun fuzzyQuery(name: String): MutableList<DataBean>? {
        var data: MutableList<DataBean>?
        withContext(Dispatchers.IO) {
            data = database?.dataDao()?.fuzzyQuery(name)
        }
        return data
    }
}